package com.qfedu.lc.user.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qfedu.lc.common.param.PayAdd;
import com.qfedu.lc.common.vo.R;
import com.qfedu.lc.core.alipay.AliPayUtil;
import com.qfedu.lc.user.entity.MoneyLog;
import com.qfedu.lc.user.entity.PayLog;
import com.qfedu.lc.user.entity.Wallet;
import com.qfedu.lc.user.mapper.MoneyLogMapper;
import com.qfedu.lc.user.mapper.PayLogMapper;
import com.qfedu.lc.user.mapper.WalletMapper;
import com.qfedu.lc.user.service.WalletService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 6.用户钱包表 服务实现类
 * </p>
 *
 * @author wgz
 * @since 2024-06-11
 */
@Service
public class WalletServiceImpl extends ServiceImpl<WalletMapper, Wallet> implements WalletService {

    @Autowired
    private WalletMapper walletMapper;

    @Autowired
    private MoneyLogMapper moneyLogMapper;


    @Autowired
    private PayLogMapper payLogMapper;

    @Override
    public boolean subWallet(Integer userId, Integer money) {
        // 1 查看账户余额

        QueryWrapper<Wallet> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("user_id",userId);
        Wallet wallet = walletMapper.selectOne(queryWrapper);

        if (wallet==null  || wallet.getMoney()<money){ // 没有账户  账户余额不足

            return  false;
        }
        // 2 削减账户余额
        wallet.setMoney(wallet.getMoney() - money);

        int num = walletMapper.updateById(wallet);
        if (num<=0){
            return false;
        }

        // 3 记录账户日志
        MoneyLog moneyLog = new MoneyLog();

        moneyLog.setUserId(userId);
        moneyLog.setMoney(money);
        moneyLog.setType(2);//减少
        moneyLog.setInfo("购买会员");
        moneyLog.setFlag("会员等级");
        moneyLog.setCtime(new Date());


        moneyLogMapper.insert(moneyLog);


        return true;
    }

    @Override
    public R rechargeWalletByZhiFuBao(PayAdd payAdd) {


        // 写入充值记录
        PayLog payLog = new PayLog();

        payLog.setUserId(payAdd.getUserId());
        payLog.setPayType(2);// 支付宝支付
        float money = payAdd.getMoney()*100;
        payLog.setMoney((int) money);

        payLog.setTitle("支付宝充值");

        // z开头表示支付宝订单
        String no = "z" + System.currentTimeMillis()+ RandomUtil.randomNumbers(6);

        payLog.setNo(no);

        // `status` int(11) DEFAULT NULL COMMENT '支付状态：1.未支付 2.已支付 3.取消支付 4.超时支付 5.退款 ',
        //  `ctime` datetime DEFAULT NULL COMMENT '创建时间',
        //  `utime` datetime DEFAULT NULL COMMENT '更新时间',
        payLog.setStatus(1);
        payLog.setCtime(new Date());
        payLog.setUtime(new Date());

        int num = payLogMapper.insert(payLog);

        if (num>0){

            // 调用支付宝支付
            String html = AliPayUtil.createPay(no, money, "爱贝充值");

            return R.ok(html);// 前端看到支付宝 界面
        }


        return R.fail("支付失败");
    }
}
